﻿using DLL.MSDM.Entity.Param;
using DLL.Net60.MotionCtrl.ControlBase;
using DLL.NET70.Infrastructure.Prism;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Standard.Infrastructure.Log;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Module.MSDMMotionShell.ViewModels
{
    public class PUC_MSDMMotionParamShellViewModel : CMVVMBase, IRegionMemberLifetime
    {
        private void Navigate(string region, string navigatePath)
        {
            if (navigatePath != null)
            {
                RegionManager.RequestNavigate(region, navigatePath);
            }
        }

        //用来保存已经加载的界面
        private ObservableCollection<object> _views = new ObservableCollection<object>();

        public ObservableCollection<object> Views
        {
            get { return _views; }
            set { SetProperty(ref _views, value); }
        }

        public bool KeepAlive
        {
            get
            {
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                //return true;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        private IMotionCtrlBase _nMotionCtrl;
        private IContainerExtension _container;

        public PUC_MSDMMotionParamShellViewModel(
            IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IContainerExtension container,
            IMotionCtrlBase motionCtrl,
            IApplicationCommands applicationCommands
            ) : base(regionManager, eventAggregator, applicationCommands)
        {
            _container = container;
            _nMotionCtrl = motionCtrl;
            NavigateCommand = new DelegateCommand<string>(Navigate);
        }

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            try
            {
                journal = navigationContext.NavigationService.Journal;
                GoForwardCommand.RaiseCanExecuteChanged();
                GoBackCommand.RaiseCanExecuteChanged();
                var obj = navigationContext.Parameters["motionCtrl"] as IMotionCtrlBase;
                if (obj != null)
                {
                    _nMotionCtrl = obj;
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("进入控制卡参数编辑界面，出现异常：" + e.Message);
            }
        }

        public DelegateCommand BackCommand { get; private set; }

        public DelegateCommand<string> NavigateCommand { get; private set; }

        private void Navigate(string viewName)
        {
            if (viewName == "PUC_MCP")
            {
                viewName = MCPName;
            }
            Navigate("MotionParamRegion", viewName);
        }

        private int cartType = 1;
        private string MCPName = "";//控制参数名称

        public override void Loaded()
        {
            NavicateInitialize();

            base.Loaded();
        }

        public override void UnLoaded()
        {
            base.UnLoaded();
        }

        private void NavicateInitialize()
        {
            string navigatePath = "";
            string contentRegion = "";
            navigatePath = "PUC_MCP_Vince1x6";
            contentRegion = "CardParamRegion";
            CWR_MotionCardType wrcard = new CWR_MotionCardType();
            CMotionCardType card = new CMotionCardType();
            wrcard.ReadParam();
            card = wrcard.Obj;
            if (card != null)
            {
                if (card.DeviceType == 0)
                {
                    //6工位配药机
                    if (card.MotionCardType == 0)
                    {
                        //1110AND 6110
                        MCPName = "PUC_MCP_TMCM1110And6110";
                    }
                    else
                    {
                        //1110 and 6214
                        MCPName = "PUC_MCP_TMCM1110And6214";
                    }
                }
                else
                {
                    //4工位配药机
                    if (card.MotionCardType == 0)
                    {
                        MCPName = "PUC_MCP_Vince1x6";
                    }
                    else
                    {
                        MCPName = "PUC_MCP_VinceCan";
                    }
                }
            }
            navigatePath = MCPName;
            Navigate(contentRegion, navigatePath);
            contentRegion = "AxisParamRegion";
            navigatePath = "PUC_AxisesParam";
            Navigate(contentRegion, navigatePath);
            contentRegion = "HomeParamRegion";
            navigatePath = "PUC_HomeParam";
            Navigate(contentRegion, navigatePath);
            contentRegion = "SpeedParamRegion";
            navigatePath = "PUC_SpeedSetParam";
            Navigate(contentRegion, navigatePath);
            contentRegion = "IOParamRegion";
            navigatePath = "PUC_IOSetParam";
            Navigate(contentRegion, navigatePath);
        }
    }
}